<?php
declare (strict_types = 1);

namespace app\login\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class User extends Model
{
    //
    public function login($username)
    {
//        查找并返回符合条件的内容
        return $this->where('username',$username)->find();
    }
    public function userAdd($phone)
    {
//        判断数据库是否存在此电话号码
        $res = $this->where('phone',$phone)->find();
        if ($res)
        {
            return false;
        }
//        若不存在则将号码加入数据库
        $list = [
            'username'=>'',
            'password'=>'',
            'phone'=>$phone
        ];
         return $this->insert($list);
    }

    public function updateuse($data)
    {
        $username = cache('username');
//        判断是否登录
        $res = $this->where('username',$username)->find();
        if ($res)
        {
            //        判断原密码是否正确
            if ($res['password']==$data['old'])
            {
                $res['password']=$data['pass'];
                return $res->save();
            }
            return 0;
        }
        return -1;
    }

    public function phone($phone)
    {
        $username = cache('username');
//        判断是否登录
        $res = $this->where('username',$username)->find();
        if ($res)
        {
//            修改电话
            $res['password']=$phone;
            return $res->save();
        }
        return false;
    }
}
